package edu.hawaii.myisern.action;

import static org.junit.Assert.assertEquals;
import org.junit.Test;
import com.meterware.httpunit.Button;
import com.meterware.httpunit.WebConversation;
import com.meterware.httpunit.WebForm;
import com.meterware.httpunit.WebLink;
import com.meterware.httpunit.WebResponse;
import com.meterware.httpunit.WebTable;

/**
 * Demonstrates that the CollaborationsActionBean is working.
 * 
 * @author Edward Kim
 * @author JianFei Liao
 * @author KaYee Leung
 */
public class TestCollaborationsActionBean {
  /** Get the test host. */
  private String testHost = System.getProperty("test.host");
  
  /**
   * Tests to view all collaborations.
   * 
   * @throws Exception If error occur.
   */
  @Test
  public void testViewAllCollaborations() throws Exception {
    WebConversation conversation = new WebConversation();
    
    // retrieves the index.jsp page and check the title
    String url = this.testHost + "myisern-gold";
    WebResponse response = conversation.getResponse(url);
    assertEquals("Checking index.jsp retrieval", "MyISERN 1.3 - Home", response.getTitle());
        
    // login the system
    WebForm loginForm = response.getFormWithID("loginForm");
    loginForm.setParameter("username", "isern");
    loginForm.setParameter("password", "password");
    response = loginForm.submit();
    
    // check if the user is login successfully by clicking the "View All Collaborations" link again
    WebLink link = response.getLinkWith("View All Collaborations");
    response = link.click();
    assertEquals("Clicking link after login", 
        "MyISERN 1.3 - View All Collaborations", response.getTitle());
    
    // check the collaborations table
    WebTable collaborationsTable = response.getTableWithID("collaborationsTable");
    // check the first collaboration in the table
    String name = collaborationsTable.getCellAsText(0, 0);
    assertEquals("Checking collaboration table", "UH-IESE-SIM-NARA", name);
    
    // clicks on the first collaboration to edit
    link = response.getLinkWith(name);
    response = link.click();
    // the page should become editCollaboration.jsp
    assertEquals("Clicking a collaboration to edit", 
        "MyISERN 1.3 - Edit Collaboration", response.getTitle());
    
    // checks if the current collaboration being edited is correct
    WebForm editForm = response.getFormWithID("editSingleCollaborationForm");
    name = editForm.getParameterValue("collaboration.name");
    assertEquals("Checking editing collaboration name", "UH-IESE-SIM-NARA", name);
  
    // changes the organization attribute and save it
    editForm.setParameter("organizationsString", "University of Hawaii, University of Maryland");
    Button button = editForm.getButtonWithID("saveButton");
    button.click();
    response = conversation.getCurrentPage();
    // the page should now go back to collaborations.jsp
    assertEquals("Checking save after changing", 
        "MyISERN 1.3 - View All Collaborations", response.getTitle());
    
    // tries to add a new collaboration
    link = response.getLinkWith("Add New Collaboration");
    response = link.click();
    // the page should become addCollaboration.jsp
    assertEquals("Click to add a new collaboration", 
        "MyISERN 1.3 - Add Collaboration", response.getTitle());
    // fill in each field and tries to submit the change each time
    WebForm addForm = response.getFormWithID("addSingleCollaborationForm");
    button = addForm.getButtonWithID("saveButton");
    button.click();
    addForm.setParameter("collaboration.name", "New Collaboration");
    button.click();
    addForm.setParameter("organizationsString", "One Collab Org");
    button.click();
    addForm.setParameter("organizationsString", "One Collab Org, Two Collab Org");
    button.click();
    addForm.setParameter("typesString", "Academic");
    button.click();
    addForm.setParameter("yearsString", "Invalid Years");
    button.click();
    addForm.setParameter("yearsString", "3000");
    button.click();
    addForm.setParameter("yearsString", "2000");
    button.click();
    response = conversation.getCurrentPage();
    // the page should be still addCollaborations.jsp 
    assertEquals("Checking save after adding with incomplete data", 
        "MyISERN 1.3 - Add Collaboration", response.getTitle());
    // tries to edit a collaboration via the link from sanity report
    link = response.getLinkWith("Sanity Report");
    response = link.click();
    // the page should become report.jsp
    assertEquals("Check the sanity report", "MyISERN 1.3 - Report", response.getTitle());
    
    // tries to click on the invalid organizations link
    WebTable reportTable = response.getTableWithID("reportTable");
    int row = 0;
    // loop thru the table to find the link we want
    // only loops for 50 times to prevent infinite looping when the data got change
    while (row < 50) {
      if ("UHM in collaboration: UKL-UH".equals(reportTable.getCellAsText(row, 0))) {
        break;
      }
      row++;
      if (row == 50) {
        System.out.println("Data is modified, record cannot be found.");
      }
    }
    String linkString = reportTable.getCellAsText(row, 0);
    assertEquals("Checking the invalid link for collaboration", 
        "UHM in collaboration: UKL-UH", linkString);
    link = response.getLinkWith(linkString);
    response = link.click();
    // the page should become editCollaboration.jsp
    assertEquals("Clicking sanity report link to edit",
        "MyISERN 1.3 - Edit Collaboration", response.getTitle());
    // verifies the editing collaboration
    editForm = response.getFormWithID("editSingleCollaborationForm");
    name = editForm.getParameterValue("collaboration.name");
    assertEquals("Checking the editing collaboration", "UKL-UH", name);
  } 
}